﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Jumpton.SFA.Server.Entity;
using Jumpton.Persistent;
using System.Data;


namespace Jumpton.SFA.Server.BusinessRule.MasterData
{
    public class FormManager : BusinessLogic
    {
        public FORMS Load(int ID)
        {
            return (FORMS)ObjectRow.Load(typeof(FORMS), string.Format("this.ID={0}", ID));
        }

        public FORMS LoadID(string Code)
        {
            return (FORMS)ObjectRow.Load(typeof(FORMS), string.Format("this.FormCode='{0}'", Code));
        }

        public FORMS Load(string code, decimal domainID, decimal ID)
        {
            return (FORMS)ObjectRow.Load(typeof(FORMS), string.Format("this.FormCode='{0}' and this.DOMAINID={1} and this.ID<>'{2}'", code, domainID, ID));
        }


        public FORMS LoadName(string Name, decimal DomainID)
        {
            return (FORMS)ObjectRow.Load(typeof(FORMS), string.Format("this.FormName='{0}' AND this.DOMAINID={1}", Name, DomainID));
        }

        public FORMS LoadDomianID(string Code, decimal DomainID)
        {
            return (FORMS)ObjectRow.Load(typeof(FORMS), string.Format("this.FormCode='{0}' AND this.DOMAINID={1}", Code, DomainID));
        }

        public static ObjectTable GetFORMS(decimal damainid)
        {
            return new ObjectTable(typeof(FORMS), "this.DOMAINID='" + damainid + "' and this.ACTIVE = '1'");
        }

        public static DataTable GetFormTree(decimal DOMAINID)
        {
            string Query = string.Format(@"select F.ID,F.CODE AS FCODE,f.NAME as FNAME,B.CODE AS BCODE,M.CODE AS MCODE,C.CODE AS CCODE,B.NAME AS BNAME, M.NAME AS MNAME,C.NAME AS CNAME from MANUFACTURERS M,FORMS F,BRANDS B,CATEGORIES C
            where F.DOMAIN_ID={0}
	            AND F.BRAND_CODE=B.CODE
	            AND F.CATEGORIE_CODE=C.CODE
	            AND F.MANUFACTURER_CODE=M.CODE
	            AND F.ACTIVE=1 AND B.ACTIVE=1 AND C.ACTIVE=1 AND M.ACTIVE=1
	            ORDER BY MCODE,CCODE,BCODE,FCODE", DOMAINID);
            return Jumpton.Data.SqlData.OpenSql(Query).Tables[0];
        }

        public static ObjectTable GetFORMS(decimal damainid,string where)
        {
            return new ObjectTable(typeof(FORMS), "this.DOMAINID='" + damainid + "' and this.ACTIVE = '1' " + where);
        }

        public DataTable GetFormDynamic(string whereString)
        {
            string Query = string.Format(@"select distinct   FM.DOMAIN_ID,
		                                            FM.CODE AS FORM_CODE,
		                                            FM.NAME AS FORM_NAME,
		                                            BD.NAME AS BRAND_NAME,
		                                            MA.NAME AS MANUFACTURER_NAME,
		                                            CG.NAME AS CATEGORIES_NAME,
                                                    FM.ACTIVE,
                                                    FM.ID 
		                                        FROM FORMS FM
                                                inner JOIN MANUFACTURERs MA
                                                on MA.CODE = FM.MANUFACTURER_CODE AND MA.DOMAIN_ID=FM.DOMAIN_ID
                                                inner JOIN BRANDS BD
                                                on BD.CODE = FM.BRAND_CODE AND BD.DOMAIN_ID=FM.DOMAIN_ID
                                                inner JOIN CATEGORIES CG
                                                on CG.CODE = FM.CATEGORIE_CODE  AND CG.DOMAIN_ID=FM.DOMAIN_ID 
                                                {0}", whereString);
            return Jumpton.Data.SqlData.OpenSql(Query).Tables[0];
        }

        /// <summary>
        /// 保存制造商表信息
        /// </summary>
        /// <param name="city"></param>
        /// <returns></returns>
        public Boolean Save(FORMS form)
        {
            try
            {
                if (this.Load(form.FormCode, form.DOMAINID, form.ID) != null)
                {
                    throw new Exception("代码已存在");
                }

               

                form.Save();
                
                return true;
            }
            catch (Exception ex)
            {
                this.message = ex.Message;
                throw ex;
            }
            
        }
    }
}

